// 引入接口文件
import { addUuseeServ,updateUuseeServ,deleteUuseeServ,checkUuseeServ} from './livePlatformListServ.js'

// 对象和JSON字符串互转
import { parse } from 'qs'
// 弹出消息框
import { message } from 'antd'
// 日期处理对象
import moment from 'moment'
/* isEmpty 对象是否为空
 * isArray 对象是否数组
 * cloneDeep 对象深拷贝
 */
import { isEmpty, isArray, cloneDeep, merge } from 'lodash'
// 引入路由对象
import { hashHistory } from 'dva/router'

import { calculate_object_name } from 'utils/upload'
import { makeAddress } from "utils/util"

// 初始默认状态
const defultState = {
  	loading: false,
  	actionType:'',
	biz: {
		queryForm: {}
	},
	modalObj:{
		visible:false,
	},
	uusee:{}
}

// 声明module
const tmpModule = {
	// 默认状态
	state: cloneDeep(defultState),
	// 入口函数(先执行)
	subscriptions: {
		setup({ dispatch, history }) {
			history.listen(({ pathname, query }) => {
				// 页面路由匹配时执行
				if ('' + tmpModule.pathname === '' + pathname) {

				} // end if
			}) // end listen
		},
	},

	// 方法列表
	effects: {
		//新增直平台
		*addOrEditUusee({ payload }, { put, call, select }){
			try{
				var {uusee,modalObj,biz,actionType} = yield select(state => state[tmpModule.namespace])
				let server = actionType == 'edit' ? updateUuseeServ:addUuseeServ
				const {success,data} = yield call(server,uusee)
				if(success){
					message.success(`${actionType == 'edit'?'编辑':'新增'}成功！`);
					modalObj.visible = !modalObj.visible;
					uusee={}
					actionType = ""
					payload.resetFields()
					yield put({ type: 'updateStore', payload: {modalObj,uusee:{},actionType} });
					biz.refreshListDatas(true)
				}
			}catch(e){console.log(e)}
		},
		//查询直播平台是否存在
		*checkUusee({ payload }, { put, call, select }){
			var {uusee,modalObj,biz,actionType} = yield select(state => state[tmpModule.namespace])
			if(actionType=='add'){
				let {success,data} = yield call(checkUuseeServ,uusee.uuseeAppId)
				if(success){
					yield put({ type: 'addOrEditUusee', payload: {...payload} });
				}
			}else{
				yield put({ type: 'addOrEditUusee', payload: {...payload} });
				
			}
		},

		//删除
		*deleteIUusee({ payload }, { put, call, select }){
			try{
				
				var {biz,actionType} = yield select(state => state[tmpModule.namespace])
				// let server = actionType == 'edit' ? updateUuseeServ:addUuseeServ
				const {success,data} = yield call(deleteUuseeServ,{id:payload.id})
				if(success){
					message.success(`删除成功！`);
					actionType = ""
					yield put({ type: 'updateStore', payload: {actionType} });
					biz.refreshListDatas(true)
				}
			}catch(e){console.log(e)}
		}

	},

	// 存入状态机
	reducers: {
		updateStore(preState, action) {
			return Object.assign({}, preState, action.payload)
		},
	}
}

export default tmpModule
